package com.android.mylib;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;

import com.haowan123.qinmeiren.appota2.Constants;

public class Interface {
	private Context context;
	private Intent intent=new Intent(Constants.UPDATE_PROGRESS_ACTION);
	private boolean savedInfo=false;
	private SharedPreferences shared;
	public Interface(Context context){
		this.context=context;
		shared = context.getSharedPreferences("sys_info", Activity.MODE_PRIVATE);
	}
	
	public native int init(String deviceID, String packageName, String SDCard);
    public native void download(String packageName, String path, String progressFunctionName, String installFunctionName);

    public void installFunc(){
        Log.d("installFunc", "INSTALLING ...");
        intent.setAction(Constants.DOWNLOAD_COMPLETE_ACTION);
        context.sendBroadcast(intent);
    }

    public void progressFunc(double downloaded, double total){
        Log.d("progressFunc", "Download "+downloaded + "/" + total);
//        try {
//			Thread.sleep(500);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
        intent.setAction(Constants.UPDATE_PROGRESS_ACTION);
        intent.putExtra(Constants.CURRENT_PROGRESS, downloaded);
        intent.putExtra(Constants.TOTAL_PROGRESS, total==0.0f?1f:total);
        if(!savedInfo && total >1000){
        	shared.edit().putLong("sys_size", (long) (total)).commit();
        	savedInfo = true;
        	Log.e("","save system size:"+total);
        }
        context.sendBroadcast(intent);
    }
}